package P1线性结构;

import java.util.Comparator;
import java.util.Iterator;

//线性结构用顺序存储方式实现的线性表
public class ArrayList<E> implements List<E> , Iterable<E>{
    private static int DEFAULT_CAPACITY = 10;   //默认最小数组容量=10
    private E[] data;   //存储元素的容器 data.length 表示容器的最大容量
    private int size;   //有效元素的个数 size <= data.length

    //构造函数 创建容量默认为10的数组 size初始化为0
    public ArrayList() {
        data = (E[]) new Object[DEFAULT_CAPACITY];
        size = 0;
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public void add(int index, E element) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("add index out of bounds");
        }
        if (element == null) {
            throw new IllegalArgumentException("element can not be null");
        }
        //判断线性表是否已满
        if (size == data.length) {
            resize(data.length * 2);
        }
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = element;
        size++;
    }

    //扩容/缩容 操作private 不需要被外界访问
    private void resize(int newLength) {
        E[] newData = (E[]) new Object[newLength];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    @Override
    public void remove(E element) {
        int index = indexOf(element);
        if (index != -1) {
            remove(index);
        }
    }

    @Override
    public E remove(int index) {
        if (index < 0 || index >= size) {   //间接包含了表为空的情况
            throw new IllegalArgumentException("remove index out of bounds");
        }
        E ret = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        //此时也需要考虑一下容量的问题 缩容问题
        if (size == data.length / 4 && data.length > DEFAULT_CAPACITY) {
            resize(data.length / 2);
        }
        return ret;
    }

    @Override
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("get index out of bounds");
        }
        return data[index];
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            return -1;
        }
        for (int i = 0; i < size; i++) {
            if (data[i].equals(element)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) != -1;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public E set(int index, E element) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("set index out of bounds");
        }
        if (element == null) {
            throw new IllegalArgumentException("element can not be null");
        }
        E ret = data[index];
        data[index] = element;
        return ret;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
        data = (E[]) new Object[DEFAULT_CAPACITY];
        size = 0;
    }

    @Override
    public List<E> sublist(int fromIndex, int toIndex) {
        //[fromIndex,toIndex)
        if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) {
            throw new IllegalArgumentException("0 <= fromIndex <= toIndex <= size");
        }
        ArrayList<E> subList = new ArrayList<>();
        for (int i = fromIndex; i < toIndex; i++) {
            subList.add(data[i]);
        }
        return subList;
    }

    @Override
    public void swap(int i, int j) {
        if (i < 0 || i >= size || j < 0 || j >= size) {
            throw new IllegalArgumentException("i or j out of bounds");
        }
        E temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }

    @Override
    public void sort(Comparator<E> comparator) {
        if (comparator == null) {
            throw new IllegalArgumentException("comparator can not be null");
        }
        //选择排序
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                if (comparator.compare(data[i], data[j]) > 0) {
                    swap(i, j);
                }
            }
        }
    }

    //两个重写的方法
    //toString "[]" , "[1, 2, 3, 4]"
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        if (isEmpty()) {
            sb.append(']');
        } else {
            for (int i = 0; i < size; i++) {
                sb.append(data[i]);
                if (i == size - 1) {
                    sb.append(']');
                } else {
                    sb.append(',');
                    sb.append(' ');
                }
            }
        }
        return sb.toString();
    }

    //equals 比ArrayList是否相等 同类型 同长度 同内容
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj instanceof ArrayList) {
            ArrayList<E> other = (ArrayList<E>) obj;
            if (this.size == other.size) {
                E[] v1 = this.data;
                E[] v2 = other.data;
                //逐个比较元素
                int p1 = 0;
                int p2 = 0;
                while (p1 < size) {
                    if (!v1[p1].equals(v2[p2])) {
                        return false;
                    }
                    p1++;
                    p2++;
                }
                return true;    //同类型 同长度 同内容
            }
            return false;
        }
        return false;
    }

    //返回该ArrayList的迭代器
    @Override
    public Iterator<E> iterator() {
        return new ArrayListIterator();
    }
    //内部类 迭代器
    private class ArrayListIterator implements Iterator<E> {
        //循环的初始化
        private int cur = 0;
        @Override
        public boolean hasNext() {  //是否结束
            //循环的继续条件
            return cur < size;
        }

        @Override
        public E next() {   //取出当前元素 再向后移动
            //循环体+变化量
            return data[cur++];
        }
        /*
        for (int cur = 0; cur < size; cur++) {
            data[cur];
        }
        */
    }
}

